package cn.wolfcode.wolf2w.business.controller.admin;

import cn.wolfcode.wolf2w.business.api.domain.AnswerContent;
import cn.wolfcode.wolf2w.business.query.AnswerContentQuery;
import cn.wolfcode.wolf2w.business.service.IAnswerContentService;
import cn.wolfcode.wolf2w.common.core.domain.R;
import cn.wolfcode.wolf2w.common.core.utils.poi.ExcelUtil;
import cn.wolfcode.wolf2w.common.core.web.controller.BaseController;
import cn.wolfcode.wolf2w.common.log.annotation.Log;
import cn.wolfcode.wolf2w.common.log.enums.BusinessType;
import cn.wolfcode.wolf2w.common.security.annotation.RequiresPermissions;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;


/**
 * 答案内容Controller
 * 
 * @author zhuyaohua
 * @date 2024-10-01
 */
@RestController
@RequestMapping("/admin/contents")
public class AnswerContentAdminController extends BaseController
{
    @Autowired
    private IAnswerContentService answerContentService;

    /**
     * 查询答案内容列表
     */
    @RequiresPermissions("content:content:list")
    @GetMapping("/list")
    public R<IPage<AnswerContent>> list(AnswerContentQuery qo) {
        return R.ok(answerContentService.queryPage(qo));
    }


    /**
     * 导出答案内容列表
     */
    @RequiresPermissions("content:content:export")
    @Log(title = "答案内容", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AnswerContent answerContent)
    {
        List<AnswerContent> list = answerContentService.list();
        ExcelUtil<AnswerContent> util = new ExcelUtil<AnswerContent>(AnswerContent.class);
        util.exportExcel(response, list, "答案内容数据");
    }

    /**
     * 获取答案内容详细信息
     */
    @RequiresPermissions("content:content:query")
    @GetMapping(value = "/{id}")
    public R<AnswerContent> getInfo(@PathVariable("id") Long id)
    {
        return R.ok(answerContentService.getById(id));
    }

    /**
     * 新增答案内容
     */
    @RequiresPermissions("content:content:add")
    @Log(title = "答案内容", businessType = BusinessType.INSERT)
    @PostMapping
    public R<?> add(@RequestBody AnswerContent answerContent)
    {
        return toAjax(answerContentService.save(answerContent));
    }

    /**
     * 修改答案内容
     */
    @RequiresPermissions("content:content:edit")
    @Log(title = "答案内容", businessType = BusinessType.UPDATE)
    @PutMapping
    public R<?> edit(@RequestBody AnswerContent answerContent)
    {
        return toAjax(answerContentService.updateById(answerContent));
    }

    /**
     * 删除答案内容
     */
    @RequiresPermissions("content:content:remove")
    @Log(title = "答案内容", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public R<?> remove(@PathVariable Long[] ids)
    {
        return toAjax(answerContentService.removeByIds(Arrays.stream(ids).collect(Collectors.toList())));
    }
}
